﻿@{
    ViewBag.Title = "检查项添加";
}
<style>
    .btn-group {
        margin-top: 0px !important;
        text-align: left !important;
    }
</style>
<link href="~/Content/select2.css" rel="stylesheet" />
<script src="~/Scripts/select2.js"></script>
<div class="container" style="margin-top:30px;margin-bottom:20px">
    <div class="row">
        <form class="form-horizontal bv-form" id="mainForm">
            <div class="col-lg-12 col-sm-12 mb25">
                <div class="form-group">
                    <label for="RoleType" class="col-sm-2 control-label">角色类型：</label>
                    <div class="col-sm-8">
                        @Html.DropDownList("RoleType", null, new { @class = "form-control selectpicker  show-tick" })
                    </div>
                </div>
                <div class="form-group ">
                    <label for="parentName" class="col-sm-2 control-label"><em class="required">*</em>分步名称：</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="ParentName" name="ParentName" value="" placeholder="请选择类别">
                        <input type="hidden" class="form-control" id="ParentId" name="ParentId" value="">
                        <input type="hidden" class="form-control" id="Level" name="Level" value="">
                    </div>
                </div>
                <div class="form-group Name" style="display:none">
                    <label for="Name" class="col-sm-2 control-label"><em class="required">*</em>分项名称：</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="Name" name="Name" placeholder="请输入检查类别">
                    </div>
                </div>
                <div class="col-lg-12 col-sm-12 form-group Remark" style="display:none">
                    <label class="col-lg-2 col-sm-2 control-label"><em class="required">*</em>具体要求：</label>
                    <div class="col-lg-10 col-sm-10">
                        <textarea name="Remark" id="Remark" placeholder="请输入检查内容" class="form-control" rows="4"></textarea>
                    </div>
                </div>
                <div class="form-group Remark" style="display:none">
                    <label for="RectificationManName" class="col-sm-2 control-label">整改负责人：</label>
                    <div class="col-sm-8">
                        <select id="RectificationManName1" name="RectificationManName1" multiple></select>
                        <input type="hidden" name="RectificationManName" id="RectificationManName" />
                    </div>
                </div>
                <div class="form-group Remark" style="display:none">
                    <label for="SalesPerson" class="col-sm-2 control-label">销项人：</label>
                    <div class="col-sm-8">
                        <select class="form-control" name="SalesPerson" id="SalesPerson">
                            <option value="XCJL">现场监理</option>
                        </select>
                    </div>
                </div>
                <div class="form-group Remark" style="display:none">
                    <label for="ScoreRange" class="col-sm-2 control-label">分值：</label>
                    <div class="col-sm-8">
                        <select class="combox" id="ScoreRange1" name="ScoreRange1" multiple>
                            <option value="10">10</option>
                            <option value="5">5</option>
                            <option value="3">3</option>
                            <option value="2">2</option>
                            <option value="1">1</option>
                            <option value="0">0</option>
                        </select>
                        <input type="hidden" name="ScoreRange" id="ScoreRange" />
                    </div>
                </div>
                <div class="form-group Remark" style="display:none">
                    <label for="ScoreCompany" class="col-sm-2 control-label">得失分单位：</label>
                    <div class="col-sm-8">
                        <select class="combox" id="ScoreCompany1" name="ScoreCompany1" multiple></select>
                        <input type="hidden" name="ScoreCompany" id="ScoreCompany" />
                    </div>
                </div>
                <div class="form-group Remark" style="display:none">
                    <label for="ScorePerson" class="col-sm-2 control-label">得失分个人：</label>
                    <div class="col-sm-8">
                        <select class="combox" id="ScorePerson1" name="ScorePerson1" multiple></select>
                        <input type="hidden" name="ScorePerson" id="ScorePerson" />
                    </div>
                </div>
                @*<div class="form-group Remark" style="display:none">
                        <label for="ConstructionUnitRange" class="col-sm-2 control-label">施工单位范围：</label>
                        <div class="col-sm-8">
                            <select class="combox" id="ConstructionUnitRange" name="ConstructionUnitRange" multiple></select>
                        </div>
                    </div>*@
                <div class="form-group ">
                    <label for="sort" class="col-sm-2 control-label">排序：</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="Sort" name="Sort" placeholder="请输入阶段排序">
                    </div>
                </div>
            </div>
            <div class="col-lg-12 col-sm-12 mb25" style="margin-top:57px">
                <div class="col-lg-6 col-sm-6" style="text-align:right">
                    <input type="button" name="submit" id="save" lay-submit lay-filter="sub" value="提&nbsp;&nbsp;&nbsp;交" class="layui-btn layui-btn-small button-base bg-save" />
                </div>
                <div class="col-lg-6 col-sm-6">
                    <input type="button" value="取&nbsp;&nbsp;&nbsp;消" class="cancel layui-btn layui-btn-small button-base bg-cancel ">
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    $(function () {
        var RectificationPeople =@Html.Raw(ViewBag.RectificationPeople);
        var RectificationCompany =@Html.Raw(ViewBag.RectificationCompany);
        //得分
        $('#ScoreRange1').select2({
            placeholder: "请至少选择一个",
            tags: true,
            createTag: function (decorated, params) {
                return null;
            },
            width: '492px'
        });
        //整改负责人
        $('#RectificationManName1').select2({
            placeholder: "请至少选择一个",
            tags: true,
            createTag: function (decorated, params) {
                return null;
            },
            width: '492px',
            data:RectificationPeople
        });
        //得失分单位
        $('#ScoreCompany1').select2({
            placeholder: "请至少选择一个",
            tags: true,
            createTag: function (decorated, params) {
                return null;
            },
            width: '492px',
            data:RectificationCompany
        });
        //得失分个人
        $('#ScorePerson1').select2({
            placeholder: "请至少选择一个",
            tags: true,
            createTag: function (decorated, params) {
                return null;
            },
            width: '492px',
            data:RectificationPeople
        });
    });

    $("#ParentName").click(function () {
        var roleType=$("#RoleType").val();
        if(roleType!=""){
            layer.open({
                type: 2,
                title: GetLayerTitle("检查项-所属父级选择"),
                shadeClose: true, //点击遮罩关闭层
                area: ['500px', '600px'],
                content: '/CheckItem/SelectParent?roleType='+roleType,
            });
        }else{
            parent.layer.alert("请选择角色类型");
            return;
        }
    });
    function parentChange() {
        if ($("#Level").val() == 3) {
            $(".Name").hide();
            $(".Remark").show();
        } else if ($("#Level").val() == 1 || $("#Level").val() == 2) {
            $(".Remark").hide();
            $(".Name").show();
        } else {
            $(".Name").show();
            $(".Remark").show();
        }
    }
    window.onload = function () {
        $('#mainForm').bootstrapValidator({
            excluded: [':hidden'],//[':disabled', ':hidden', ':not(:visible)'] //设置隐藏组件可验证
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                ParentName: {
                    validators: {
                        notEmpty: {
                            message: '父级不能为空'
                        }
                    }
                },
                Name: {
                    validators: {
                        notEmpty: {
                            message: '检查类别不能为空'
                        },
                        stringLength: {
                            max: 100,
                            message: '检查类别不能超过100字符'
                        },
                        //regexp: {
                        //    regexp: /^[a-zA-Z0-9\u4e00-\u9fa5-]+$/i,
                        //    message: "检查名称由汉字字母数字及-组成"
                        //}
                    }
                },
                Remark: {
                    validators: {
                        notEmpty: {
                            message: '检查内容不能为空'
                        }
                    }
                },
                Description: {
                    validators: {
                        stringLength: {
                            max: 254,
                            message: '检查方式不能超过254个字符'
                        }
                    }
                },
                RoleType: {
                    validators: {
                        stringLength: {
                            max: 100,
                            message: '角色类型不能超过100个字符'
                        },
                        notEmpty: {
                            message: '角色类型不能为空'
                        }
                    }
                },
                Sort: {
                    validators: {
                        stringLength: {
                            max: 10,
                            message: '排序不能超过10字符'
                        }, regexp: {
                            regexp: /^[0-9]+$/i,
                            message: "排序由数字组成"
                        }
                    }
                }
            }
        });
    }
    layui.use(['layer', 'form', 'element', 'laypage'], function () {
        var layer = layui.layer, form = layui.form, element = layui.element, $ = layui.jquery, laypage = layui.laypage;
        //监听提交
        form.on('submit(sub)', function (data) {
            //对表单进行验证
            var bv = $('#mainForm').data('bootstrapValidator');
            bv.validate();
            if (bv.isValid()) {
                var parentId = $("#ParentId").val();
                if (parentId == 0 || parentId == "0") {
                    $("#ParentName").val('');
                }else if($("#Level").val() == 3){
                    //得分
                    var reslist=$("#ScoreRange1").select2("data"); //多选
                    if(reslist.length<=0)
                    {
                        parent.layer.alert("请选择得分");
                        return;
                    }else{
                        var strScore="";
                        for(var i=0;i<reslist.length;i++){
                            if(reslist[i].selected){
                                strScore+=reslist[i].id+",";
                            }
                        }
                        $("#ScoreRange").val(strScore.substring(0,strScore.length-1));
                    }

                    //整改负责人
                    var rectificationManNamelist=$("#RectificationManName1").select2("data"); //多选
                    if(rectificationManNamelist.length<=0)
                    {
                        parent.layer.alert("请选择整改负责人");
                        return;
                    }else{
                        var strrectificationManName="";
                        for(var i=0;i<rectificationManNamelist.length;i++){
                            if(rectificationManNamelist[i].selected){
                                strrectificationManName+=rectificationManNamelist[i].id+",";
                            }
                        }
                        $("#RectificationManName").val(strrectificationManName.substring(0,strrectificationManName.length-1));
                    }

                    //得失分单位
                    var scoreCompanylist=$("#ScoreCompany1").select2("data"); //多选
                    if(scoreCompanylist.length<=0)
                    {
                        parent.layer.alert("请选择得失分单位");
                        return;
                    }else{
                        var strscoreCompany="";
                        for(var i=0;i<scoreCompanylist.length;i++){
                            if(scoreCompanylist[i].selected){
                                strscoreCompany+=scoreCompanylist[i].id+",";
                            }
                        }
                        $("#ScoreCompany").val(strscoreCompany.substring(0,strscoreCompany.length-1));
                    }

                    //得失分个人
                    var scorePersonlist=$("#ScorePerson1").select2("data"); //多选
                    if(scorePersonlist.length<=0)
                    {
                        parent.layer.alert("请选择得失分个人");
                        return;
                    }else{
                        var strscorePerson="";
                        for(var i=0;i<scorePersonlist.length;i++){
                            if(scorePersonlist[i].selected){
                                strscorePerson+=scorePersonlist[i].id+",";
                            }
                        }
                        $("#ScorePerson").val(strscorePerson.substring(0,strscorePerson.length-1));
                    }
                }
                var search = $("form").serialize();
                $.post("Add", search, function (data) {
                    if (data.Flag) {
                        parent.layer.msg("添加成功", { time: 1000, icon: 1 }, function () {
                            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                            parent.layer.close(index);//关闭窗口
                            parent.layer.close(alertIndex);
                        });
                    } else {
                        layer.alert(data.Message, { icon: 2 });
                    }
                });
                return true;
            }
        });
        //关闭弹出iframe层
        $('.cancel').on("click", function () {
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            parent.layer.close(index);//关闭窗口
        });
    });

</script>